package com.yanqu.road.dao.impl.activity.zhenhuan;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.zhenhuan.secondProcess.CrossZhenHuanPartyData;
import com.yanqu.road.entity.activity.zhenhuan.secondProcess.CrossZhenHuanPartyJoinData;
import com.yanqu.road.entity.activity.zhenhuan.secondProcess.CrossZhenHuanSecondUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossZhenHuanSecondUserImpl extends TempDataObjectDao<CrossZhenHuanSecondUserData> {
    public static String getCrossZhenHuanSecondUserDataSql = "select * from `t_c_zhenhuan_second_user` where `activityId`=?";

    public Map<Long, CrossZhenHuanSecondUserData> getCrossZhenHuanSecondUserData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, CrossZhenHuanSecondUserData> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCrossZhenHuanSecondUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossZhenHuanSecondUserData data = parse(rs);
                dataMap.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateCrossZhenHuanSecondUserDataSql = "update `t_c_zhenhuan_second_user` set level=?,prestige=? where activityId=? and userId=?";

    @Override
    protected boolean updateData(CrossZhenHuanSecondUserData crossZhenHuanSecondUserData) {
        crossZhenHuanSecondUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossZhenHuanSecondUserData.getLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, crossZhenHuanSecondUserData.getPrestige()));
        para.put(i++, new DbParameter(Types.INTEGER, crossZhenHuanSecondUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossZhenHuanSecondUserData.getUserId()));
        if (getDb().executeNoneQuery(updateCrossZhenHuanSecondUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossZhenHuanSecondUserDataSql = "insert into `t_c_zhenhuan_second_user`(activityId,userId,level,prestige) values (?,?,?,?)";

    @Override
    protected boolean addData(CrossZhenHuanSecondUserData crossZhenHuanSecondUserData) {
        crossZhenHuanSecondUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossZhenHuanSecondUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossZhenHuanSecondUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossZhenHuanSecondUserData.getLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, crossZhenHuanSecondUserData.getPrestige()));
        if (getDb().executeNoneQuery(insertCrossZhenHuanSecondUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossZhenHuanSecondUserData parseData(ResultSet rs) throws SQLException {
        CrossZhenHuanSecondUserData crossZhenHuanSecondUserData = new CrossZhenHuanSecondUserData();
        crossZhenHuanSecondUserData.setActivityId(rs.getInt("activityId"));
        crossZhenHuanSecondUserData.setUserId(rs.getLong("userId"));
        crossZhenHuanSecondUserData.setLevel(rs.getInt("level"));
        crossZhenHuanSecondUserData.setPrestige(rs.getLong("prestige"));

        return crossZhenHuanSecondUserData;
    }
}
